/*
 * @Author: your name
 * @Date: 2021-11-05 10:02:05
 * @LastEditTime: 2021-11-16 14:00:10
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \macc.admin.vue\src\projectCommon\store\modules\ports\index.js
 */
const state = {
  // 步骤1
  // 已选设备列表
  selectList: [],
  //  选择设备分类 数量统计
  selectTypeObj: null,
  // 步骤二
  vlanList: [],
  // 已选端口的数据 类型区分
  selectPortTypeData: {},
  // 接口配置 构造对象
  portObjList: [],
  // 已经选择的端口数据
  portSelect: {},
  // 信息展示列表
  confInfo: {},
};
const mutations = {
  CANCEL_SELECT(state) {
    state.selectList = [];
  },
  ADD_VLAN_LIST(state, payload) {},
  DEL_VLAN_LIST(state, payload) {},
  UPDATE_VLAN_CONF(state, payload) {
    state.vlanList = payload;
  },
  SET_SELECT_SNS(state, payload) {
    state.selectList = payload;
  },
  SET_SELECT_SNS_TYPE(state, payload) {
    state.selectTypeObj = payload;
  },
  SET_SELECT_PORT_TYPE(state, payload) {
    if (payload.type) {
      state.selectPortTypeData[payload.type] = payload.portData;
    }
    if (!payload.portData) {
      delete state.selectPortTypeData[payload.type];
    }
  },
  SET_PORT_OBJ_LIST(state, payload) {
    // debugger;
    console.log(payload, '接受对象999999999999999999');
    // debugger;
    // let firstObj = payload[Object.keys(payload)[0]][0];
    let firstObj = Object.values(payload).filter((item) => !!item)[0];

    if (firstObj.type != 'Access') {
      state.portObjList.push(payload);
      return;
    }
    let target = state.vlanList.find((item) => item.vlanId == firstObj.vlan);
    console.log(target, '目标的下标');
    // 找不到 相应的vlanid
    if (!target) {
      state.vlanList.push({
        vlanId: firstObj.vlan,
      });
      state.portObjList.push(payload);
    }
    // 找到了绑定相应vlanid的配置数据
    if (target) {
      // 新加
      console.log(
        state.portObjList,
        '全部的额portObj的数据7878787887878787878'
      );
      for (let item of state.portObjList) {
        for (let key in item) {
          // debugger;
          console.log(item, 'item数据');
          console.log(key, 'key的值数据');
          console.log(item[key][0], '遍历出来的对象值数据');
          console.log(payload[key], '遍历出来payload的对象值数据');
          if (payload[key] && item[key][0].vlan == payload[key][0].vlan) {
            item[key] = [...item[key], ...payload[key]];
            item = { ...item };
          }
        }
      }
      state.portObjList = [...state.portObjList];
    }
  },
  SET_PORT_SELECT(state, payload) {
    console.log('放入SET_PORT_SELECT数据');
    if (payload.selectPortData) {
      state.portSelect[payload.productClass] = payload.selectPortData;
    }
    state.portSelect = { ...state.portSelect };
    console.log(state.portSelect);
  },
  CANCEL_PORT_SELECT(state, payload) {
    delete state.portSelect[payload];
    state.portSelect = { ...state.portSelect };
    console.log('“需要重置的设备类型');

    delete state.selectPortTypeData[payload];
    state.selectPortTypeData = { ...state.selectPortTypeData };
    console.log(state.selectPortTypeData);
  },
  RESET_PORT_SELECT(state) {
    (state.selectPortTypeData = {}), (state.portSelect = {});
  },
  SET_CONF_INFO(state, payload) {
    state.confInfo = payload;
  },
  // 删除 接口配置信息
  DEL_PORT_OBJ_LIST(state, payload) {
    let index = state.portObjList.indexOf((item) => item == payload);
    state.portObjList.splice(index, 1);
  },
  DEL_PORT_OBJ(state, payload) {
    // console.log(state.portObjList)
    state.portObjList.forEach((ele) => {
      console.log(ele, 'DEL_PORT_OBJ操作删除对象的端口对象');
      if (ele[payload.type]) {
        ele[payload.type].forEach((item, index) => {
          if (item.order == payload.port) {
            ele[payload.type].splice(index, 1);
          }
          if (ele[payload.type].length == 0) {
            delete ele[payload.type];
          }
        });
      }
    });
    state.portObjList = [...state.portObjList];
    console.log('更新后的portObjList为：', state.portObjList);
  },
  RESET_PORT_OBJ_LIST() {
    state.portObjList = [];
  },
};
const actions = {
  // 类型区分
  setSelectPortTypeData({ commit }, params) {
    commit('SET_SELECT_PORT_TYPE', params);
  },
  // 接口配置数据  构造的数组对象
  setPortObjList({ commit }, params) {
    commit('SET_PORT_OBJ_LIST', params);
  },
  // 更新 vlan 配置数据
  updateVlanConf({ commit }, params) {
    commit('UPDATE_VLAN_CONF', params);
  },
  // 已经选中端口的数据
  setPortSelect({ commit }, params) {
    commit('SET_PORT_SELECT', params);
  },
  resetPortSelect({ commit }) {
    commit('RESET_PORT_SELECT');
  },
  setConfInfo({ commit }, params) {
    commit('SET_CONF_INFO', params);
  },
  delPortObjList({ commit }, params) {
    commit('DEL_PORT_OBJ_LIST', params);
  },
  delPortObj({ commit }, params) {
    commit('DEL_PORT_OBJ', params);
  },
  resetPortObjList({ commit }) {
    commit('RESET_PORT_OBJ_LIST');
  },
};
const getters = {
  getSelectList(state) {
    return state.selectList;
  },
};
export default {
  namespaced: true, //开启命名空间，更好的区分使用stage,防止不同stage模块中的方法重名
  state,
  getters,
  mutations,
  actions,
};
